package com.dongdongsongcan.app.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.common.DDMessageDef;
import com.dongdongsongcan.app.control.UserControl;
import com.dongdongsongcan.app.fragment.MainFragment;
import com.dongdongsongcan.app.fragment.OrderListFragment;
import com.dongdongsongcan.app.fragment.UserFragment;
import com.su.dolphin.core.AppThread;
import com.su.dolphin.core.notification.DpMessageCenter;
import com.su.dolphin.core.notification.DpMessageCenter.MCMessage;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.mgr.DpFragmentManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

public class MainActivity extends BaseActivity {


    View mMain_tab_btn1;
    View mMain_tab_btn2;
    View mMain_tab_btn3;

    private ImageView mMain_tab_btn1_img;
    private ImageView mMain_tab_btn2_img;
    private ImageView mMain_tab_btn3_img;
    private ViewFlipper mMain_viewflipper_tab;
    private TextView main_tab_btn1_text;
    private TextView main_tab_btn2_text;
    private TextView main_tab_btn3_text;

    public static void startFrom(Context context) {
        DpActivityManager.i().startActivity(context, MainActivity.class);
        ((Activity) context).finish();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        DpMessageCenter.i().register(this, DDMessageDef.N_USER_CHANGE);
        super.onCreate(savedInstanceState);
        setTitlebarEnable(false);
        setContentView(R.layout.activity_main);

    }

    @Override
    public void initView() {
        mMain_tab_btn1 = findViewById(R.id.main_tab_btn1);
        mMain_tab_btn2 = findViewById(R.id.main_tab_btn2);
        mMain_tab_btn3 = findViewById(R.id.main_tab_btn3);

        main_tab_btn1_text = (TextView) findViewById(R.id.main_tab_btn1_text);
        main_tab_btn2_text = (TextView) findViewById(R.id.main_tab_btn2_text);
        main_tab_btn3_text = (TextView) findViewById(R.id.main_tab_btn3_text);

        mMain_tab_btn1_img = (ImageView) findViewById(R.id.main_tab_btn1_img);
        mMain_tab_btn2_img = (ImageView) findViewById(R.id.main_tab_btn2_img);
        mMain_tab_btn3_img = (ImageView) findViewById(R.id.main_tab_btn3_img);

        mMain_viewflipper_tab = (ViewFlipper) findViewById(R.id.main_viewflipper_tab);

        DpFragmentManager.i().replaceFragment(this, R.id.main_fragment_tab1,
                MainFragment.class);
        DpFragmentManager.i().replaceFragment(this, R.id.main_fragment_tab2,
                OrderListFragment.class);
        DpFragmentManager.i().replaceFragment(this, R.id.main_fragment_tab3,
                UserFragment.class);
    }

    @Override
    public void setListener() {
        mMain_tab_btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //有空对tab做一个封装吧
                mMain_viewflipper_tab.setDisplayedChild(0);
                mMain_tab_btn1_img.setImageResource(R.drawable.main_ordermeal_select);
                mMain_tab_btn1_img.setPadding(1,1,1,1);
                mMain_tab_btn2_img.setImageResource(R.drawable.main_orderlist);
                mMain_tab_btn3_img.setImageResource(R.drawable.main_usercenter);
                mMain_tab_btn2_img.setPadding(5,5,5,5);
                mMain_tab_btn3_img.setPadding(5,5,5,5);

                main_tab_btn1_text.setTextColor(0xFF00C6AF);
                main_tab_btn2_text.setTextColor(0xFF000000);
                main_tab_btn3_text.setTextColor(0xFF000000);

                mMain_tab_btn1.setBackgroundColor(0xFFEEEEEE);
                mMain_tab_btn2.setBackgroundColor(0xFFFFFFFF);
                mMain_tab_btn3.setBackgroundColor(0xFFFFFFFF);


            }
        });
        mMain_tab_btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mMain_viewflipper_tab.setDisplayedChild(1);
                mMain_tab_btn1_img.setImageResource(R.drawable.main_ordermeal);
                mMain_tab_btn2_img.setImageResource(R.drawable.main_orderlist_select);
                mMain_tab_btn2_img.setPadding(1,1,1,1);
                mMain_tab_btn3_img.setImageResource(R.drawable.main_usercenter);
                mMain_tab_btn1_img.setPadding(5,5,5,5);
                mMain_tab_btn3_img.setPadding(5,5,5,5);
                main_tab_btn1_text.setTextColor(0xFF000000);
                main_tab_btn2_text.setTextColor(0xFF00C6AF);
                main_tab_btn3_text.setTextColor(0xFF000000);

                mMain_tab_btn1.setBackgroundColor(0xFFFFFFFF);
                mMain_tab_btn2.setBackgroundColor(0xFFEEEEEE);
                mMain_tab_btn3.setBackgroundColor(0xFFFFFFFF);

            }
        });
        mMain_tab_btn3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!UserControl.i().userExist()) {
                    LoginActivity.startFrom(MainActivity.this);
                } else {
                    jumpToUserTab();
                }
            }
        });
    }

    public void jumpToUserTab() {
        mMain_viewflipper_tab.setDisplayedChild(2);
        mMain_tab_btn1_img.setImageResource(R.drawable.main_ordermeal);
        mMain_tab_btn2_img.setImageResource(R.drawable.main_orderlist);
        mMain_tab_btn3_img.setImageResource(R.drawable.main_usercenter_select);
        mMain_tab_btn3_img.setPadding(1,1,1,1);
        mMain_tab_btn2_img.setPadding(5,5,5,5);
        mMain_tab_btn1_img.setPadding(5,5,5,5);
        main_tab_btn1_text.setTextColor(0xFF000000);
        main_tab_btn2_text.setTextColor(0xFF000000);
        main_tab_btn3_text.setTextColor(0xFF00C6AF);

        mMain_tab_btn1.setBackgroundColor(0xFFFFFFFF);
        mMain_tab_btn2.setBackgroundColor(0xFFFFFFFF);
        mMain_tab_btn3.setBackgroundColor(0xFFEEEEEE);
    }

    @Override
    public void initData() {


    }

    @Override
    public void onNotify(MCMessage mcmessage) {
        if (mcmessage.notificationID == DDMessageDef.N_USER_CHANGE) {
            jumpToUserTab();
        }
    }

    private boolean mShouldExit;

    @Override
    public void onBackPressed() {
        if (mShouldExit) {
            finish();
        } else {
            mShouldExit = true;
            TipsUtil.toast("再按一次退出咚咚送餐");
            AppThread.uiPost(new Runnable() {
                @Override
                public void run() {
                    mShouldExit = false;
                }
            }, 3000);
        }
        super.onBackPressed();
    }


}
